﻿package{
	import flash.xml.XMLDocument;
	import flash.events.Event;

	 import flash.net.URLRequest;
	 import flash.net.URLLoader;
	 import flash.net.URLLoaderDataFormat;
	 import flash.net.URLVariables;
	 import flash.net.URLRequestMethod;
	 public class LoadCharacter{
	
		private var strokes:Array=new Array()//存放笔顺信息
		private var beziers:String="";//贝塞尔曲线信息
		private var character:String="";//汉字
		private var loader:URLLoader;//
		private var url:URLRequest;//
		private var flag:Boolean;
		private var pinyin:String;
		private var englishDefinition:String;
		private var sentence:String;
		public function LoadCharacter(s:String){
			flag=false;
			//url=new URLRequest("http://127.0.0.1/languagenuts/datatoflash2.php"); //从datatoflash这个中获得数据。
			url=new URLRequest("http://192.168.1.200/ZS/datatoflash2.php"); //从datatoflash这个中获得数据。

			url.method = URLRequestMethod.POST; //用post方法传递参数
			loader = new URLLoader(); 
			//监听数载加载完成事件 
			loader.dataFormat = URLLoaderDataFormat.VARIABLES;
			//事件相应函数 
			var values:URLVariables = new URLVariables(); //设置传递的参数属性
			values.message=s;//参数名message，值s
			url.data = values; //
	
		}
		function getFlag():Boolean{
			return this.flag;
			}
		function loaded(e:Event){ 
			try{
				
				trace(loader.data.cc);
				
				if(loader.data.beziers==""){
					flag=false;
					//throw new Error("beziers is null!");
					
					}
				else if(loader.data.character==""){
					flag=false;
					//throw new Error("character is null!");
					}
				else if(loader.data.strokes==""){
					flag=false;
					//throw new Error("strokes is null!");
				}
				else{
					flag=true;
					//throw new Error("data load fail!");
					
					}
				
				
				this.strokes=loader.data.strokes.split("");//设置笔顺信息
				this.beziers=loader.data.beziers;//设置贝塞尔数据
				this.character=loader.data.character;//设置汉字
				
				}catch(errObject:Error){
				trace(errObject.message);
				trace('load failed!------------------------>');
				}
				} 
		public function cwpLoad(){
			
			loader.addEventListener(Event.COMPLETE,loaded); //添加load事件
			try{
				loader.load(url);
			}catch(errObject:Error){
				trace(errObject.message);
				trace('load failed!-------------------------------->');
				}
			
			}
		public function getCharacter():String{
			//trace(character);
			return this.character;
			}
		public function getStrokes():Array{
			
			return this.strokes;
			}
		public function getBeziers():Array{
			
			var bezier:Array=new Array();
		try{
			var a:Array=new Array();//把读到的贝塞尔曲线存储到数组中去3#d1;d2#...到[[d1][d2]][...]
			//trace(this.beziers);
			a=this.beziers.split("#");
			if((a.length -1)!=this.strokes.length){
				trace(this.strokes.length);
				trace(a.length);
				throw new Error("data error! data of strokes and beziers is wrong");
				}
			
			bezier.push(a[0]);
			//trace("a"+a.length);
			for(var i:int=1;i<a.length;i++){
				var ttt:Array=new Array();
				//trace(i);
				var b:Array=new Array();
				b=a[i].split("/");
				
				//trace(b[0]);
				var be:Array=new Array();
				be=b[0].split(";");
				
				
				for(var j:int=0;j<be.length;j++){
					var be1:Array=new Array();
					be1=be[j].split(",");
					//trace(be1.length);
					//bezier[i-1].push(be1);
					if(be1.length%6!=2){
						trace(be1.length);
						throw new Error("data error! data of beziers is wrong be"+j);
						}
					for(var kk=0;kk<be1.length;kk=kk+2){
						be1[kk]-=50;
						}
					ttt.push(be1);
					
				}
				
				
				var d:Array=b[1].split(";");
				if(d.length!=2){
					throw new Error("data error! data of startP and endP is wrong");
					}
					
				var zb:Array=new Array();
				for(var k:int=0;k<2;k++){
					var e:Array=new Array();
					e=d[k].split(",");
					e[0]-=50;
					zb.push(e);
				}
				var tt:Array=new Array();
				tt.push(ttt);
				tt.push(zb);
				bezier.push(tt);
				//trace(bezier[i]);
				
				}
			//trace(bezier.length);
			}catch(errObject:Error){
				trace(errObject.message);
				}
			return bezier;
			}
		
	}
}